import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;


public class NodeInventory {
	
	Player player;
	ImageIcon image = new ImageIcon(getClass().getResource("/Pics/bag.png"));
	ArrayList<ItemKind> listWithItems = new ArrayList<ItemKind>();
	protected int currentInventoryLevel = 0;
	protected int finalInventoryLevel = 0;
	String[][] inventoryLevels = new String[20][6];
	final int ENLARGE_VALUE = 100;
	InterfaceFrame frame;
	JComponent lastButton;
	CustomButton money = new CustomButton();
	CustomButton item1 = new CustomButton();
	CustomButton item2 = new CustomButton();
	CustomButton item3 = new CustomButton();
	CustomButton item4 = new CustomButton();
	CustomButton item5 = new CustomButton();
	CustomArrowButton up = new CustomArrowButton("Up");
	CustomArrowButton down = new CustomArrowButton("Down");
	
	
	NodeInventory(Player player, InterfaceFrame frame){
		
		listWithItems = player.inventory.getItems();
		this.player = player;
		this.frame = frame;
		createInventoryLevels();
		up.addActionListener(new ItemListener());
		down.addActionListener(new ItemListener());
		item1.addActionListener(new ItemListener());
		item2.addActionListener(new ItemListener());
		item3.addActionListener(new ItemListener());
		item4.addActionListener(new ItemListener());
		item5.addActionListener(new ItemListener());
		money.addActionListener(new ItemListener());
		item1.addMouseListener(new TheMouseListener());
		item2.addMouseListener(new TheMouseListener());
		item3.addMouseListener(new TheMouseListener());
		item4.addMouseListener(new TheMouseListener());
		item5.addMouseListener(new TheMouseListener());
		money.addMouseListener(new TheMouseListener());
		up.addMouseListener(new TheMouseListener());
		down.addMouseListener(new TheMouseListener());
		
	}
	
	public ImageIcon getPic(){
		return image;
	}
	
	public void showInventoryLevel(int level){
		
		createInventoryLevels();
		frame.nface.popOptions.setLayout(new FlowLayout(FlowLayout.LEFT,0,0));
		
		money.setText(player.money + "x Guld");
		frame.nface.popOptions.add(money);
		
		if(inventoryLevels[level][0] != null)
			item1.setText(inventoryLevels[level][0]);
		if(inventoryLevels[level][1] != null)
			item2.setText(inventoryLevels[level][1]);
		if(inventoryLevels[level][2] != null)
			item3.setText(inventoryLevels[level][2]);
		if(inventoryLevels[level][3] != null)
			item4.setText(inventoryLevels[level][3]);
		if(inventoryLevels[level][4] != null)
			item5.setText(inventoryLevels[level][4]);
		
		
		if(inventoryLevels[level][0] != null)
			frame.nface.popOptions.add(item1);
		else
			frame.nface.popOptions.remove(item1);
		if(inventoryLevels[level][1] != null)
			frame.nface.popOptions.add(item2);
		else
			frame.nface.popOptions.remove(item2);
		if(inventoryLevels[level][2] != null)
			frame.nface.popOptions.add(item3);
		else
			frame.nface.popOptions.remove(item3);
		if(inventoryLevels[level][3] != null)
			frame.nface.popOptions.add(item4);
		else
			frame.nface.popOptions.remove(item4);
		if(inventoryLevels[level][4] != null)
			frame.nface.popOptions.add(item5);
		else
			frame.nface.popOptions.remove(item5);
		
		if(currentInventoryLevel != finalInventoryLevel && listWithItems.size() > 5)
			frame.nface.popOptions.add(down);
		else
			frame.nface.popOptions.remove(down);
		if(currentInventoryLevel != 0)
			frame.nface.popOptions.add(up);
		else
			frame.nface.popOptions.remove(up);
	}
	
	//Skapar en större array, behövs då vi inte vet hur många items spelaren har.
    private void enlarge(){
    	
    	int newLength = 1 + inventoryLevels.length + ENLARGE_VALUE * inventoryLevels.length/100;
    	String[][] newArray = new String[newLength][5];
    	
    	for(int index = 0; index < inventoryLevels.length; index++){
    		for(int j = 0; j < 5; j++)
    			newArray[index][j] = inventoryLevels[index][j];
    	}
    	
    	inventoryLevels = newArray;
    	
    }
    
    public void createInventoryLevels(){
    	
    	inventoryLevels = new String[20][5];
    	listWithItems = player.inventory.getItems();
    	
    	//Undersöker vilken sista Leveln är.
    	if(listWithItems.size()%5 == 0)
    		finalInventoryLevel = listWithItems.size()/5 - 1;
    	else
    		finalInventoryLevel = listWithItems.size()/6;
    	
    	String level[] = new String[5];
    	int index = 0;
    	
    	for(int i = 0; i <= finalInventoryLevel; i++){
    		
    		//Om inventoryLevels arrayen är full, skapa en större!
    		if(inventoryLevels.length <= index)
    			enlarge();
    		
    		//Lopar fram en sträng till varje index som innehåller hur många items och namnet.
    		for(int j = 0; j < 5; j++){
    			
    			level[i] = player.inventory.itemCount(listWithItems.get(index)) + "x " + new Item(listWithItems.get(index)).getName();
    			inventoryLevels[i][j] = level[i];
    			index++;
    			
    			//Ifall vi kommit till slutet bryt.
    			if(index >= listWithItems.size()){
    				break;
    			}
    			
    		}
    		
    	}
    	
    }
    
	
	private class ItemListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			if(e.getSource().equals(down)){

				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = down;
				down.setForeground(Color.BLUE);
				currentInventoryLevel++;
				showInventoryLevel(currentInventoryLevel);
			}
			else if(e.getSource().equals(up)){

				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = up;
				up.setForeground(Color.BLUE);
				currentInventoryLevel--;
				showInventoryLevel(currentInventoryLevel);
			}

			else if(e.getSource().equals(money)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = money;
				money.setForeground(Color.BLUE);
				frame.nface.textArea.setText("Hur mycket deg du har att slösa bort i affärn");
			}
			else if(e.getSource().equals(item1)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = item1;
				item1.setForeground(Color.BLUE);
				int index = currentInventoryLevel * 5 + 0;
				frame.nface.textArea.setText((new Item(listWithItems.get(index)).description));
			}
			else if(e.getSource().equals(item2)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = item2;
				item2.setForeground(Color.BLUE);
				int index = currentInventoryLevel * 5 + 1;
				frame.nface.textArea.setText((new Item(listWithItems.get(index)).description));
			}
			else if(e.getSource().equals(item3)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = item3;
				item3.setForeground(Color.BLUE);
				int index = currentInventoryLevel * 5 + 2;
				frame.nface.textArea.setText((new Item(listWithItems.get(index)).description));
			}
			else if(e.getSource().equals(item4)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = item4;
				item4.setForeground(Color.BLUE);
				int index = currentInventoryLevel * 5 + 3;
				frame.nface.textArea.setText((new Item(listWithItems.get(index)).description));
			}
			else if(e.getSource().equals(item5)){
				if(lastButton != null)
					lastButton.setForeground(Color.BLACK);
				lastButton = item5;
				item5.setForeground(Color.BLUE);
				int index = currentInventoryLevel * 5 + 4;
				frame.nface.textArea.setText((new Item(listWithItems.get(index)).description));
			}
			
			frame.getContentPane().repaint();
		}
		
	}
	
	private class CustomButton extends JButton{
		
		private static final long serialVersionUID = 1L;
		

		CustomButton(){
			this.setContentAreaFilled(false);
			this.setForeground(Color.BLACK);
		}
		
		@Override
		public Dimension getMinimumSize(){
			return new Dimension(306,50);	
		}
		@Override
		public Dimension getMaximumSize(){
			return new Dimension(306,50);	
		}
		@Override
		public Dimension getPreferredSize(){
			return new Dimension(306,50);	
		}
		
	}
	
	private class CustomArrowButton extends JButton{
		
		private static final long serialVersionUID = 1L;
		CustomArrowButton(String str){

			this.setContentAreaFilled(false);
			this.setForeground(Color.BLACK);
			setText(str);
		}
		
		@Override
		public Dimension getMinimumSize(){
			return new Dimension(153,50);	
		}
		@Override
		public Dimension getMaximumSize(){
			return new Dimension(153,50);	
		}
		@Override
		public Dimension getPreferredSize(){
			return new Dimension(153,50);	
		}
		
	}
	
	private class TheMouseListener implements MouseListener{

		@Override
		public void mouseClicked(MouseEvent e){
		}

		@Override
		public void mouseEntered(MouseEvent e) {
		}

		@Override
		public void mouseExited(MouseEvent e) {
		}
		
		@Override
		public void mousePressed(MouseEvent e) {
			frame.nface.popPrime.show(frame, 50, 55);
		}

		@Override
		public void mouseReleased(MouseEvent e) {
			
		}
		
	}
	
}
